package com.csw;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2022/11/17 20:48
 */
public class GameWin extends JFrame {
    //窗口属性
    int width=768;
    int height=1000;
    //背景对象
    Bg bg = new Bg();
    //游戏状态 1 运行中 2商店 3失败 4胜利
    static int state;
    Line line = new Line(this);
    //定义空的图片(先把图片画进空的图片再一次加载进主窗口中解决双缓存)
    Image offScreenImage;
    //存储金矿,石块
    List<Object> objectList =new ArrayList<Object>();

    {
        for (int i = 0; i < 11; i++) {
            //是否重叠
            boolean isOverLay=false;
            //0到1随机数值
            double random = Math.random();
            //存放各种类型金矿
            Gold gold;
            if (random<0.4){
                gold=new Gold();
            }else if (random<0.7){
                gold=new GoldMini();
            }else {
                gold=new GoldPlus();
            }
            for (Object object:objectList){
                if (gold.getRec().intersects(object.getRec())){
                    isOverLay=true;
                }
            }
            //没有重叠才加入集合
            if (!isOverLay){
                objectList.add(gold);
            }
        }

        for (int i = 0; i < 7; i++) {
            //是否重叠
            boolean isOverLay=false;
            Rock rock=new Rock();
            for (Object object:objectList){
                if (rock.getRec().intersects(object.getRec())){
                    isOverLay=true;
                }
            }
            //没有重叠才加入集合
            if (!isOverLay){
                objectList.add(rock);
            }
        }
    }
    void launch() {
        //是否可见
        this.setVisible(true);
        //设置窗口大小
        this.setSize(width, height);
        //设置窗口位置居中
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("尚学堂黄金矿工");
        //添加关闭窗口的方法
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //鼠标左键
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                //游戏状态 1 运行中 2商店 3失败 4胜利
                switch (state){
                    case 0:
                        //鼠标右键
                        if (e.getButton()==3){
                            state=1;
                        }
                        break;
                    case 1:
                        //鼠标左键
                        if (e.getButton()==1){
                            line.state=1;
                        }
                        //抓取过程这鼠标右键
                        if (e.getButton()==3&&line.state==3){
                            if (Bg.water_can){
                                if (Bg.water_Num>=1){
                                    //药水正在使用
                                    Bg.water_use=true;
                                    Bg.water_Num--;
                                }else {
                                    Bg.water_Num=0;
                                    //药水不在使用
                                    Bg.water_use=false;
                                    //药水不能使用
                                    Bg.water_can=false;
                                }
                            }
                        }
                        break;
                    case 2:
                        if (e.getButton()==1){
                            bg.isShop=true;
                        }
                        if (e.getButton()==3){
                            state=1;
                            bg.startTime=System.currentTimeMillis();
                        }
                        break;
                    case 3:
                    case 4:
                        if (e.getButton()==1){
                            state=0;
                            bg.reGame();
                            line.reGame();
                        }
                        break;
                    default:
                }


            }
        });


        while (true){

            repaint();
            nextLevel();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }
    //下一关
    public void nextLevel(){
        if (bg.gameTime()&&state==1){
            //bg一直新建的,非静态属性与对象的建立有关
            if (Bg.count>=bg.goal){
                if (Bg.level==5){
                    //成功界面
                    state=4;
                }else {
                    state=2;
                    Bg.level++;

                }


            }else {
                //失败
                state=3;
            }
            //关闭当前窗口
            dispose();
            GameWin gameWin1 = new GameWin();
            gameWin1.launch();
        }

    }

    @Override
    public void paint(Graphics g) {
        //新建画布
        offScreenImage= createImage(width, height);
        //调用画布的画笔
        Graphics offG = offScreenImage.getGraphics();
        bg.paintSelf(offG);
        if (state==1){

            for (Object object:objectList){
                object.paintSelf(offG);
            }
            line.paintSelf(offG);
        }

        //将画布一次画进窗口中解决窗口闪动
        g.drawImage(offScreenImage, 0, 0, null);
    }

    public static void main(String[] args) {
        new GameWin().launch();
    }
}
